#include "DialogTestNode.h"
#include "dialog/DatePackerDialog.h"
#include "dialog/AreaDialog.h"
#include "dialog/SingleSelectDialog.h"
#include "dialog/ToastDialog.h"
#include "dialog/ProgressDialog.h"

#define PROGRESS_ST	0
#define TOAST_TAG	1

#define NONE_ACTIVE		-1
#define PROGRESS_BTN_TAG	0
#define AREA_BTN_TAG		1
#define DATE_BTN_TAG		2
#define SINGLE_BTN_TAG		3

static const char* bloods[] = {
	"O", "A", "B", "AB"
};

static CCNode* getDialogByTag(int tag)
{
	CCScene* s = CCDirector::sharedDirector()->getRunningScene();
	if (s) {
		return s->getChildByTag(tag);
	}
	return NULL;
}

DialogTestNode::~DialogTestNode()
{
	ToastDialog* pt = (ToastDialog*)getDialogByTag(TOAST_TAG);
	CC_SAFE_RELEASE(pt);
	close_area_db();
}

bool DialogTestNode::init()
{
	bool bRet = false;
	do {
		CC_BREAK_IF(!CCNode::init());
		_menu = CCMenu::create();
		_menu->setPosition(CCPointZero);
		this->addChild(_menu);
		_active = NONE_ACTIVE;
		init_area_db(CCFileUtils::sharedFileUtils()->fullPathForFilename("dialog/area.db").c_str());
		addLbl("progress", 0, PROGRESS_BTN_TAG);
		addLbl("datepicker", 1, DATE_BTN_TAG);
		addLbl("areapicker", 2, AREA_BTN_TAG);
		addLbl("singleselect", 3, SINGLE_BTN_TAG);
		//process单独做一个按钮显示2s不带文字转2s带文字
		//其它的控件设置了数据后用toast显示2s
		bRet = true;
	}while(0);
	return bRet;
}

void DialogTestNode::showToast(const char* text)
{
	ToastDialog* p = (ToastDialog*)getDialogByTag(TOAST_TAG);
	if (!p) {
		p = ToastDialog::create(text, 2);
		p->setTag(TOAST_TAG);
		p->retain();
	} else {
		p->updateStr(text);
		p->setDismiss(2);
	}
	p->show();
}

void DialogTestNode::changeProcessText(float dt)
{
	if (ProgressDialog::defaultDialog()->tipsLength() > 0) {
		ProgressDialog::defaultDialog()->hide();
		_active = NONE_ACTIVE;
	} else {
		ProgressDialog::defaultDialog()->setTips("Hello girl!\nHow are you?");
	}
}

void DialogTestNode::btnCB(CCObject* pobj)
{
	_active = ((CCNode*)pobj)->getTag();
	switch (_active) {
	case PROGRESS_BTN_TAG:
		ProgressDialog::defaultDialog()->setTips("");
		ProgressDialog::defaultDialog()->show();
		this->schedule(schedule_selector(DialogTestNode::changeProcessText), 2.0f);
		break;
	case AREA_BTN_TAG:
	{
		AreaDialog* p = AreaDialog::create();
		p->setYesFunc(this, yesfunc_selector(DialogTestNode::yesCB));
		p->show();
	}
		break;
	case DATE_BTN_TAG:
	{
		DatePackerDialog* p = DatePackerDialog::create();
		p->setYesFunc(this, yesfunc_selector(DialogTestNode::yesCB));
		p->show();
	}
		break;
	case SINGLE_BTN_TAG:
	{
		SingleSelectDialog* p = SingleSelectDialog::create("选择你的血型", bloods, sizeof(bloods) / sizeof(bloods[0]), 1);
		p->setYesFunc(this, yesfunc_selector(DialogTestNode::yesCB));
		p->show();
	}
		break;
	}
}

void DialogTestNode::yesCB(CCObject* pobj)
{
	YesNoPortrol* p = (YesNoPortrol*)pobj;
	size_t code = p->getRetCode();
	char buf[128] = {0};
	switch(_active) {
	case AREA_BTN_TAG:
	{
		area_info_p parea = init_area_info(code);
		if (parea->country)
			sprintf(buf, "%s-%s-%s", parea->pro, parea->city, parea->country);
		else
			sprintf(buf, "%s-%s", parea->pro, parea->city);
		free_area_info(parea);
	}
		break;
	case DATE_BTN_TAG:
	{
		DatePackerDialog::date_t date;
		DatePackerDialog::convertCTimeToDate(code, &date);
		sprintf(buf, "%u年%u月%u日", date.y, date.m, date.d);
	}
		break;
	case SINGLE_BTN_TAG:
		sprintf(buf, "您选择的是%s型血", bloods[code]);
		break;
	}
	showToast(buf);
}

void DialogTestNode::addLbl(const char* str, int i, int tag)
{
	CCMenuItemFont* p =CCMenuItemFont::create(str, this, menu_selector(DialogTestNode::btnCB));
	p->setAnchorPoint(ccp(0.5, 0.5));
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	p->setPosition(ccp(winSize.width / 2, winSize.height - 50 - i * 50));
	_menu->addChild(p, 0, tag);
}
